/*
 * Copyright 2007 Jaisy Sprl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package be.jaisy.ui.gwt.widget.sample.client.table;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import be.jaisy.ui.gwt.widget.client.sink.Sink;
import be.jaisy.ui.gwt.widget.client.viewer.IContentProvider;
import be.jaisy.ui.gwt.widget.client.viewer.Viewer;
import be.jaisy.ui.gwt.widget.client.viewer.table.ITableLabelProvider;
import be.jaisy.ui.gwt.widget.client.viewer.table.TableViewer;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author Stany Blanvalet <stany.blanvalet@jaisy.be>
 */
public class TableViewerSampleSink extends Sink{

	public static SinkInfo init(){
		return new SinkInfo("TableViewerSampleSink", "<p>TableViewerSimpleSampleSink desciption</p>"){

			public Sink createInstance(){
				return new TableViewerSampleSink();
			}

			public String getColor(){
				return "blue";
			}

		};
	}
	
	public TableViewerSampleSink(){

		TableViewer table = new TableViewer();
		table.addHeader(0, "Action", false);
		table.addHeader(1, "name", true);
		table.addHeader(2, "time", false);
		table.addHeader(3, "description", true);

		table.addFilter(1);
		table.addFilter(3);

		table.setContentProvider(new IContentProvider(){
			public Object[] getElements(Object inputElement){
				return ((List)inputElement).toArray();
			}

			public void inputChanged(Viewer viewer, Object oldInput, Object newInput){
				// nothing to do
			}
		});
		table.setLabelProvider(new ITableLabelProvider(){
			
			public Object[] getRow(Object element){
				if (element instanceof ViewTo){
					ViewTo viewTo = (ViewTo)element;
					Button button = new Button("start");
					button.addClickListener(new ClickListener(){
						
						public void onClick(Widget sender){
							Window.alert("started!");
							
						}
					});
					return new Object[] {button, viewTo.getName(), viewTo.getTime(), viewTo.getDescription()};
				}
				return null;
			}
			
			public String getCellStyle(Object element, int columnIndex){
				if (element instanceof ViewTo){
					ViewTo viewTo = (ViewTo)element;
					if (viewTo.getName().indexOf("3") >= 0){
						if (columnIndex == 1){
							return "-green";
						}
					}
				}
				return null;
			}
			
			public String getRowStyle(Object element){
				if (element instanceof ViewTo){
					ViewTo viewTo = (ViewTo)element;
					if (viewTo.getName().indexOf("3") >= 0)
						return "-red";
					if (viewTo.getName().indexOf("2") >= 0)
						return "-yellow";
					if (viewTo.getName().indexOf("1") >= 0)
						return "-blue";
				}
				return null;
			}
		});
		
		table.setInput(getMockServiceResult());


		initWidget(table);
	}

	public List/* ViewTo */getMockServiceResult(){
		List/* ViewTo */result = new ArrayList/* ViewTo */();

		for (int i = 0; i < 20; i++){
			ViewTo viewTo = new ViewTo();
			viewTo.setName("viewToName_" + i);
			viewTo.setTime(new Date());
			viewTo.setDescription("viewToDescription_" + i);
			result.add(viewTo);
		}
		return result;
	}

	public class ViewTo{

		private String name;

		private Date time;

		private String description;

		public String getDescription(){
			return description;
		}

		public void setDescription(String description){
			this.description = description;
		}

		public String getName(){
			return name;
		}

		public void setName(String name){
			this.name = name;
		}

		public Date getTime(){
			return time;
		}

		public void setTime(Date time){
			this.time = time;
		}

	}

}
